import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/page/train/train_tab_details1_page.dart';
import 'package:get/get.dart';

import '../trainnew/train_line_course_details_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/14.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class TrainListItemWidget extends StatefulWidget {
  final TrainBean trainBean;
  final int index;

  TrainListItemWidget({this.trainBean, this.index});

  @override
  _TrainListItemWidgetState createState() => _TrainListItemWidgetState();
}

class _TrainListItemWidgetState extends State<TrainListItemWidget> {
  @override
  Widget build(BuildContext context) {
    return buildBodyWidget();
  }

  //每个子Item的布局构建
  Widget buildBodyWidget() {
    //水波纹点击事件监听
    return InkWell(
      //手指点击抬起时的回调
      onTap: () async {
        //打开新的页面
        // NavigatorUtils.pushPage(
        //     context,
        //     TrainListDetailsPage(
        //       approveBean: TrainApproveBean(
        //         widget.trainBean.trainId,
        //         widget.trainBean.createTime,
        //         examId: widget.trainBean.examId,
        //       ),
        //     ),
        //     callback: (value) {});

        String trainLine = widget.trainBean.trainLine;
        if (trainLine == "offline") {
          //trainLineStr = "线下";
          await Get.to(
            () => TrainTabDetails1Page(
              approveBean: TrainApproveBean(
                widget.trainBean.trainId,
                widget.trainBean.createTime,
                examId: widget.trainBean.examId,
              ),
            ),
          );
        } else {
          // trainLineStr = "线上课程学习";
          await Get.to(
            () => TrainLineCourseDetailsPage(
              trainId: widget.trainBean.trainId,
            ),
          );
        }
      },
      child: Container(
        padding: EdgeInsets.all(10),
        margin: EdgeInsets.only(top: 1.0),
        color: Colors.white,
        //线性布局左右排列
        child: Row(
          //主轴方向开始对齐 在这里是左对齐
          mainAxisAlignment: MainAxisAlignment.start,
          //交叉轴上开始对齐 在这里是顶部对齐
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //左侧的图片
            buildLeftImage(),
            //右侧的文本区域
            Expanded(child: buildRightTextArea()),
          ],
        ),
      ),
    );
  }

  ///左侧的图片区域
  Container buildLeftImage() {
    int trainStatus = widget.trainBean.trainStatus;

    String statuesStr = "";
    Color textColor = Colors.blueGrey;
    if (trainStatus == 5) {
      textColor = Colors.redAccent;
      statuesStr = "进行中";
    } else if (trainStatus == 6) {
      statuesStr = "已完成";
      textColor = Colors.green;
    }
    return Container(
      width: 4,
      height: 16,
      alignment: Alignment.center,
      margin: EdgeInsets.only(right: 8, top: 6),
      decoration: BoxDecoration(
          color: textColor,
          borderRadius: BorderRadius.all(Radius.circular(4)),
          border: Border.all(color: Colors.transparent)),
    );
  }

  ///右侧的文本区域
  Widget buildRightTextArea() {
    String trainLineStr = "";
    String trainLine = widget.trainBean.trainLine;
    if (trainLine == "offline") {
      trainLineStr = "线下";
    } else {
      trainLineStr = "线上课程学习";
    }

    int trainStatus = widget.trainBean.trainStatus;

    String statuesStr = "";
    Color textColor = Colors.blueGrey;
    if (trainStatus == 5) {
      textColor = Colors.green;
      statuesStr = "进行中";
    } else if (trainStatus == 6) {
      statuesStr = "已完成";
      textColor = Colors.green;
    } else if (trainStatus == 9) {
      statuesStr = "未开始";
      textColor = Colors.grey;
    } else if (trainStatus == 102) {
      statuesStr = "培训未开始";
      textColor = Colors.grey;
    } else if (trainStatus == 101) {
      statuesStr = "培训已结束";
      textColor = Colors.grey;
    }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          height: 4,
        ),
        Row(
          children: [
            Expanded(
              child: Text(
                "${widget.trainBean.trainName}",
                softWrap: true,
                overflow: TextOverflow.ellipsis,
                maxLines: 3,
                style: TextStyle(fontSize: 16),
              ),
            ),
            SizedBox(
              width: 10,
            ),
            Text(
              "$statuesStr",
              style: TextStyle(fontSize: 14, color: textColor),
            ),
            SizedBox(
              width: 8,
            ),
            Container(
              child: Icon(
                Icons.arrow_forward_ios,
                size: 14,
              ),
            )
          ],
        ),
        SizedBox(
          height: 8,
        ),
        Text(
          "培训方式 $trainLineStr",
          softWrap: true,
          overflow: TextOverflow.ellipsis,
          maxLines: 3,
          style: TextStyle(fontSize: 12, color: Colors.black38),
        ),
        Text(
          "培训时间 ${widget.trainBean.startStudyTime}~  ${widget.trainBean.endStudyTime}",
          softWrap: true,
          overflow: TextOverflow.ellipsis,
          maxLines: 3,
          style: TextStyle(fontSize: 12, color: Colors.black38),
        ),
        SizedBox(
          height: 8,
        ),
        Text(
          "创建时间 ${widget.trainBean.createTime}",
          softWrap: true,
          overflow: TextOverflow.ellipsis,
          maxLines: 3,
          style: TextStyle(fontSize: 12, color: Colors.black38),
        ),
        buildUserTrainStudyTips(),
      ],
    );
  }

  buildUserTrainStudyTips() {
    String userTrainStudyTips = widget.trainBean.userTrainStudyTips;
    if (userTrainStudyTips == null || userTrainStudyTips.isEmpty) {
      return SizedBox();
    }
    return Container(
      margin: EdgeInsets.only(top: 8),
      child: Text(
        userTrainStudyTips,
        softWrap: true,
        overflow: TextOverflow.ellipsis,
        maxLines: 3,
        style: TextStyle(fontSize: 12, color: Colors.black),
      ),
    );
  }
}
